<?php

namespace app\common\model;

use think\Model;
use think\Db;
class Car extends Model
{
    protected $resultSetType = '';
    // 追加属性
    protected $append = [
        'status_text',
    ];
    public static function get_list($page,$keywords,$product_id)
    {
        $where = [];
        if($keywords)
        {
            $where['c.plate_number']=['like','%'.$keywords.'%'];
        }
        if($product_id)
        {
            $car_ids=db('product')->where('id',$product_id)->value('car_ids');
            $where['c.id']=['in',$car_ids];
        }
        $where['status']='3';
        
        $field = "c.id,c.num,c.plate_number,c.name,c.contact_tel,s.store_name,c.status";
        $order = "c.status asc";
        $count = self::alias('c')->join('store s','c.store_id=s.id','left')->where($where)->count();
        $limit = 20;
        $start = ($page - 1) * $limit;
        $total = ceil($count / $limit);
        $list = self::alias('c')->join('store s','c.store_id=s.id','left')->where($where)->field($field)->order($order)->limit($start, $limit)->select();
    
        $data = [
            'total'=>$total,
            'list'=>$list
        ];
        return $data;
    }

    public static function get_info($id)
    {
        $where['id']=$id;
        $info = self::where($where)->field("*")->find();
        //图片
        $images=explode(',',$info['images']);
        foreach ($images as $key => &$value) {
            # code...
            $value=gethost().$value;
        }
        $info['imagelist']=$images;
        
        return $info;
    }

    public function getAddTimeAttr($value)
    {
        if($value){
            return date("Y-m-d H:i:s",$value);
        }else{
            return "";
        }
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = getstatus();
        return isset($list[$value-1]) ? $list[$value-1] : '';
    }
    
}
